package GUILayer;
import java.util.ArrayList;

import javax.swing.JPanel;
import javax.swing.JTabbedPane;
import javax.swing.JLabel;
import javax.swing.JComboBox;
import javax.swing.JTextField;
import javax.swing.JTable;
import javax.swing.JScrollPane;
import javax.swing.table.DefaultTableModel;

import ControlLayer.TreatCatCtr;
import ControlLayer.TreatmentCtr;
import DBLayer.DBTreatCat;
import ModelLayer.Treatment;
import ModelLayer.TreatmentCategory;

import javax.swing.DefaultComboBoxModel;
import javax.swing.JButton;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import javax.swing.ListSelectionModel;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;


public class ManageProducts extends JPanel {
	private JTextField txtSearchWord;
	private JTable tblResults;
	private TreatmentCtr treatmentCtr;
	private JButton btnSearch;
	private ArrayList<Treatment> treatments;
	private ManageTreatments pnlTreatments;
	private JButton btnAddToPackage;
	private JPanel pnlSearchProduct;
	private DefaultTableModel modelTreatment;

	/**
	 * Create the panel.
	 */
	public ManageProducts() 
	{
		setLayout(null);
		
		JTabbedPane tabbedPane = new JTabbedPane(JTabbedPane.TOP);
		tabbedPane.setBounds(10, 205, 1212, 526);
		add(tabbedPane);
		
		pnlTreatments = new ManageTreatments();
		tabbedPane.addTab("Behandlinger", null, pnlTreatments, null);
		
		ManageSaleItems pnlSaleItems = new ManageSaleItems();
		tabbedPane.addTab("Salgs produkter", null, pnlSaleItems, null);
		
		pnlSearchProduct = new JPanel();
		pnlSearchProduct.setBounds(10, 11, 967, 183);
		add(pnlSearchProduct);
		pnlSearchProduct.setLayout(null);
		
		JLabel lblProdType = new JLabel("Produkt type:");
		lblProdType.setBounds(10, 11, 86, 14);
		pnlSearchProduct.add(lblProdType);
		
		JComboBox cbProductType = new JComboBox();
		cbProductType.setModel(new DefaultComboBoxModel(new String[] {"Behandlinger"}));
		cbProductType.setBounds(106, 8, 178, 20);
		pnlSearchProduct.add(cbProductType);
		
		JLabel lblSearchWord = new JLabel("S\u00F8ge ord:");
		lblSearchWord.setBounds(317, 11, 64, 14);
		pnlSearchProduct.add(lblSearchWord);
		
		txtSearchWord = new JTextField();
		txtSearchWord.setBounds(384, 8, 211, 20);
		pnlSearchProduct.add(txtSearchWord);
		txtSearchWord.setColumns(10);
		
		JScrollPane scrollPaneResults = new JScrollPane();
		scrollPaneResults.setBounds(20, 43, 937, 94);
		pnlSearchProduct.add(scrollPaneResults);
		
		tblResults = new JTable();
		tblResults.setAutoscrolls(false);
		tblResults.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
		scrollPaneResults.setViewportView(tblResults);
		
		btnSearch = new JButton("S\u00F8g");
		btnSearch.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) 
			{
				updateResultTable();
			}
		});
		btnSearch.setBounds(605, 7, 89, 23);
		pnlSearchProduct.add(btnSearch);
		
		JButton btnVlg = new JButton("V\u00E6lg");
		btnVlg.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) 
			{
				if(tblResults.getSelectedRow() != -1)
				{
					setTreatment();
					
				}
			}
		});
		btnVlg.setBounds(172, 148, 89, 23);
		pnlSearchProduct.add(btnVlg);
		
		btnAddToPackage = new JButton("Tilf\u00F8j til pakke");
		btnAddToPackage.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) 
			{
				if(tblResults.getSelectedRow() != -1)
				{
					addToPackage();
				}
			}
		});
		btnAddToPackage.setVisible(false);
		btnAddToPackage.setBounds(423, 148, 134, 23);
		pnlSearchProduct.add(btnAddToPackage);
		
		treatmentCtr = new TreatmentCtr();

	}
	
	public void updateResultTable()
	{
		TreatCatCtr treatCatCtr = new TreatCatCtr();
		
		ArrayList<TreatmentCategory> cats = treatCatCtr.getTreatCategories();
		
		treatments = treatmentCtr.getAllTreatmentsBySearchword(txtSearchWord.getText());
		
		modelTreatment = new DefaultTableModel();
		
		modelTreatment.addColumn("ID:");
		modelTreatment.addColumn("Tittel:");
		modelTreatment.addColumn("Type:");
		modelTreatment.addColumn("Beskrivelse:");
		modelTreatment.addColumn("Pris:");
		modelTreatment.addColumn("Estimeret tid:");
		modelTreatment.addColumn("Behandligs kategori:");
		
		
		for(Treatment treat : treatments)
		{
			Object[] row = {treat.getId(), treat.getTitle(), treat.getType(), treat.getDescription(), treat.getPrice(), 
					treat.getEstimatedTime(), cats.get(treat.getTreatmentCategory().getId()-1)};
			
			modelTreatment.addRow(row);
		}
		
		tblResults.setModel(modelTreatment);
	}
	
	private void setTreatment()
	{
		Treatment selTreatment = treatments.get(tblResults.getSelectedRow());

		pnlTreatments.setTreatment(selTreatment);
	}
	
	private void addToPackage()
	{
		Treatment selTreatment = treatments.get(tblResults.getSelectedRow());

		pnlTreatments.addToPackage(selTreatment);
		pnlTreatments.setDescription(selTreatment);
	}
	
	public JPanel getPnlSearch()
	{
		return pnlSearchProduct;
	}
	
	public void setBtnVisibility(boolean state)
	{
		btnAddToPackage.setVisible(state);
	}
	
}











